package com.chengcang.api.modules.busi.dto;

import com.chengcang.api.modules.login.utils.UserHolder;
import com.chengcang.common.exception.CustomException;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Data
@Component
public class BusiOrderPageDTO implements Serializable {
    //---------------------------分页参数---------------------------
    @ApiModelProperty(value = "页码，从1开始")
    private int pageIndex;

    @ApiModelProperty(value = "每页数据量")
    private int pageSize;

    private int offset;

    //---------------------------默认查询参数---------------------------
    //需求方id
    private Long demandId;
    //履约方id
    private Long supplyId;
    //租户id
    private Long tenantId;

    //---------------------------查询参数---------------------------
    @ApiModelProperty(value = "订单状态")
    private Integer status;

    //给app提供的重置支持
    public void setStatus(Integer status){
        if(status != -1){
            this.status = status;
        }
    }

    @ApiModelProperty(value = "甲方ID")
    private Long paId;

    @ApiModelProperty(value = "省id")
    private Long provinceId;

    @ApiModelProperty(value = "城市id")
    private Long cityId;

    @ApiModelProperty(value = "地区id")
    private Long areaId;

    @ApiModelProperty(value = "接单最小时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderMinTime;

    @ApiModelProperty(value = "接单最大时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderMaxTime;

    @ApiModelProperty(value = "需求最小时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime demandMinTime;

    @ApiModelProperty(value = "需求最大时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime demandMaxTime;

    @ApiModelProperty(value = "最小单价")
    private BigDecimal minPrice;

    @ApiModelProperty(value = "最大单价")
    private BigDecimal maxPrice;

    @ApiModelProperty(value = "货品类型id,2.24弃用")
    private Long productOrTypeId;

    @ApiModelProperty(value = "货品类型id")
    private Long productId;

    //---------------------------不规范的默认排序字段常量, 没必要抽取---------------------------
    private String orderField = "bo.create_date";

    public void setOrderField(String orderField){
        this.orderField = orderField;
        if(StringUtils.isNullOrEmpty(orderField)){
            this.orderField = "bo.create_date";
        }

    }

    private String order = "desc";

    public void setOrder(String order){
        this.order = order;
        if(StringUtils.isNullOrEmpty(order)){
            this.order = "desc";
        }
    }
    /**
     * 持久层传参使用, 产品分类筛选
     */
    private List<Long> productIds;


    public void setPageSize(int size){
       this.pageSize = size;
//       try {
//           this.tenantId = Optional.ofNullable(UserHolder.getUser().getDefaultTenantId())
//                   .orElseThrow(()->new CustomException("租户id为空!"));
//       }catch (Exception e){
//           throw new CustomException("获取用户信息异常,信息:"+e.getMessage());
//       }
       this.offset =  size * (pageIndex-1);
    }

    public void setPageIndex(int index){
        this.pageIndex = index;
        this.offset =  pageSize * (index-1);
    }

}
